﻿using System;
using AccessControl.Data;
using System.Collections.Generic;
using AccessControl.Contracts.Managers;
using AccessControl.Contracts.Models;
using AccessControl.Contracts.Video;
using System.Net;
using AccessControl.Video;
using AccessControl.Contracts.Settings;
using AccessControl.Contracts.Reports;
using AccessControl.DataManagers;
using AccessControl.Gateway.Settings;
using AccessControl.Gateway.Reports;

namespace AccessControl.Gateway.Managers
{
	public class Session : NotifyPropertyChangedBase
	{
		#region Fields

		private static Session s_Instance = null;
		private IOperator _LoggedInOperator = null;
		private ISettingsService _settingsService;
		private IReportsService _reportsService;

		private DataManager _dataManager;

		#endregion

		#region Private Constructor

		private Session()
		{
			
		}

		#endregion

		#region Public Properties

		public static Session Instance
		{
			get 
			{
				lock (typeof(Session))
				{
					if (s_Instance == null)
						s_Instance = new Session();

					return s_Instance;
				}
			}
		}

		public IEntriesManager EntriesManager
		{
			get { return AccessControl.Gateway.Managers.EntriesManager.Instance; }
		}

		public IEventsManager EventsManager
		{
			get { return AccessControl.Gateway.Managers.EventsManager.Instance; }
		}

		public IMapsManager MapsManager
		{
			get { return AccessControl.Gateway.Managers.MapsManager.Instance; }
		}

		public IOperator LoggedInOperator
		{
			get { return _LoggedInOperator; }
			private set
			{
				if (_LoggedInOperator != value)
				{
					_LoggedInOperator = value;
					NotifyPropertyChanged("LoggedInOperator");
				}
			}
		}

		public ISettingsService SettingsService
		{
			get { return _settingsService; }
			private set
			{
				if (_settingsService != value)
				{
					_settingsService = value;
					NotifyPropertyChanged("SettingsService");
				}
			}
		}

		public IReportsService ReportsService
		{
			get { return _reportsService; }
			private set
			{
				if (_reportsService != value)
				{
					_reportsService = value;
					NotifyPropertyChanged("ReportsService");
				}
			}
		}
		
		#endregion	
	
		#region Public Members

		/// <summary>
		/// Logins with the specified user name.
		/// </summary>
		/// <param name="userName">Name of the user.</param>
		/// <param name="password">The password.</param>
		/// <returns></returns>
		/// <remarks></remarks>
		public bool Login(string userName, string password)
		{
			List<IOperator> operators = new List<IOperator>();

			if (_dataManager == null)
			{
				_dataManager = DataManager.Instance;
				if (!_dataManager.Initialize())
					return false;
			}

			/* Get all the operators */
			_dataManager.FillList(operators);
			foreach (IOperator myOperator in operators)
			{
				if (myOperator.UserName == userName && myOperator.Password == password)
				{
					LoggedInOperator = myOperator;
					break;
				}
			}

			/* Check if we are ok */
			if (LoggedInOperator != null)
			{
				/* Initialize the sesstion */
				SettingsService = new SettingsService();
				ReportsService = new ReportsService();
			}

			return (LoggedInOperator != null);
		}

		/// <summary>
		/// Logouts this session.
		/// </summary>
		/// <returns></returns>
		/// <remarks></remarks>
		public bool Logout()
		{
			LoggedInOperator = null;
			return true;
		}

		/// <summary>
		/// Creates the video player.
		/// </summary>
		/// <returns></returns>
		/// <remarks></remarks>
		public IVideoPlayer CreateVideoPlayer()
		{
			return new VideoPlayer();
		}
		
		#endregion
	}
}
